package com.mmall.common;

import com.mmall.util.PropertiesUtil;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.util.Objects;

/**
 * @author Paul
 * @version 0.1
 * @date 2018/8/10 0010
 */
public class RedisPool {
    /**
     * jedis连接池
     */
    private static JedisPool pool;
    /**
     * 最大连接数
     */
    private static Integer maxTotal = Integer.valueOf(PropertiesUtil.getProperty("redis.max.total", "20"));

    /**
     * 最大空闲
     */
    private static Integer maxIdle = Integer.valueOf(PropertiesUtil.getProperty("redis.max.idle", "10"));
    /**
     * 最小空闲
     */
    private static Integer minIdle = Integer.valueOf(PropertiesUtil.getProperty("redis.min.idle", "2"));
    /**
     * 系统ip
     */
    private static String ip = PropertiesUtil.getProperty("redis.ip");

    private static String password = PropertiesUtil.getProperty("redis.password");

    private static Integer redisPort = Integer.valueOf(Objects.requireNonNull(PropertiesUtil.getProperty("redis.port")));

    private static void initPool() {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(maxTotal);
        config.setMaxIdle(maxIdle);
        config.setMinIdle(minIdle);
        config.setTestOnReturn(true);
        config.setTestOnBorrow(true);
        //连接耗尽时是否阻塞
        config.setBlockWhenExhausted(false);
        pool = new JedisPool(config, ip, redisPort,1000*2,password);
    }
    static {
        initPool();
    }

    public static Jedis getJedis() {
        return pool.getResource();
    }
}
